﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YAPL.Language.Core.Exceptions;
using YAPL.Language.Core.Types;
using CSharpMath = System.Math;
namespace YAPL.Language.Core.Lib.GlobalFunctions.Math {
	public class Pow : YAPLFunction {
		private const string ARG1 = "arg1";
		private const string ARG2 = "arg2";
		public Pow() : base(FunctionNames.Pow, GCL.Get(ClassNames.GlobalScope)) { }

		[Param(0, ARG1),Param(1, ARG2)]
		public override YAPLObject Call(Scope scope) {
			ParseScope(scope);
			var arg1 = scope[ARG1];
			var arg2 = scope[ARG2];
			if (arg1 is YAPLInt && arg2 is YAPLInt) {
				var result = (int)CSharpMath.Pow((arg1 as YAPLInt).IntValue, (arg2 as YAPLInt).IntValue);
				return new YAPLInt(result);
			}
			if ((arg1 is YAPLInt || arg1 is YAPLFloat) && (arg2 is YAPLInt || arg2 is YAPLFloat)) {
				var arg1Value = (arg1 is YAPLInt) ? (arg1 as YAPLInt).IntValue : (arg1 as YAPLFloat).FloatValue;
				var arg2Value = (arg2 is YAPLInt) ? (arg2 as YAPLInt).IntValue : (arg2 as YAPLFloat).FloatValue;
				var result = CSharpMath.Pow(arg1Value, arg2Value);
				return new YAPLFloat(result);
			}
			throw new YAPLException("pow function has some invalid arguments");
		}

		public override bool CanPrecompute {
			get {
				return true;
			}
		}
	}
}
